package de.fhkoeln.avs.storage;

import java.util.ArrayList;
import java.util.List;

import com.infinitegraph.BaseEdge;
import com.infinitegraph.BaseVertex;
import com.infinitegraph.EdgeKind;
import com.infinitegraph.navigation.Guide;
import com.infinitegraph.navigation.NavigationResultHandler;
import com.infinitegraph.navigation.Navigator;
import com.infinitegraph.navigation.Path;
import com.infinitegraph.navigation.Qualifier;
import com.infinitegraph.navigation.policies.MaximumPathDepthPolicy;
import com.infinitegraph.policies.PolicyChain;


public class AvsPersistentVertex extends BaseVertex implements AvsVertex{

	private String name;
	private String owner;
	
//	private List<String> childNames = new ArrayList<String>();
	
	public AvsPersistentVertex(){
		super();
	}
	
	public AvsPersistentVertex(String name, String owner){
		this.setName(name);
		this.owner = owner;
	}
	
	public List<AvsVertex> getChilds(){
		
		List<AvsVertex> childs = new ArrayList<AvsVertex>();
		PolicyChain navigatePolicy = new PolicyChain(new MaximumPathDepthPolicy(1));
		PrintPathResultsHandler resultHandler = new PrintPathResultsHandler(childs);
		Navigator navigator = 
				this.navigate(null, 
							  Guide.SIMPLE_BREADTH_FIRST,
							  Qualifier.FOREVER,
							  Qualifier.ANY,
							  navigatePolicy,
							  resultHandler);
		navigator.start();
		navigator.stop();

		return childs;	
	}

	public void addChild(BaseVertex child, BaseEdge edge) {
		this.addEdge(edge, child, EdgeKind.OUTGOING, (short) 0);
//		childNames.add(((AvsVertex)child).getName());
	}

	public String getName() {
		fetch();
		return this.name;
	}

	public void setName(String name) {
		markModified();
		this.name = name;
	}
	
	class PrintPathResultsHandler implements NavigationResultHandler {
		List<AvsVertex> childs;
	    PrintPathResultsHandler(List<AvsVertex> childs){
	    	this.childs = childs;
	    }
	    @Override
	    public void handleResultPath(Path result, Navigator navigator) {      
	        childs.add((AvsPersistentVertex)result.getFinalHop().getVertex());    
	    }
	    @Override
	    public void handleNavigatorFinished(Navigator navigator) {}
	}

	@Override
	public void addChild(AvsVertex child) {
		// TODO Auto-generated method stub
		
	}

	public String getOwner(){
		fetch();
		return owner;
	}
	
	public void setOwner(String owner) {
		markModified();
		this.owner = owner;
	}

//	@Override
//	public List<String> getChildNames() {
//		return childNames;
//	}
}
